package com.tjp.eight.ui.fragment.jifen;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.ToatUtils;

import org.hamcrest.core.Is;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/28.
 */
public class FragmentIntegrationTask extends BaseFragment<IntegrationTaskPresenter> implements IntegrationTaskView {
    RelativeLayout rl_integration_share_get;
    LinearLayout ll_task_container;
    TextView tv_integration_count;
    TextView tv_task_in;

    @Override
    protected IntegrationTaskPresenter createPresenter() {
        return new IntegrationTaskPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_integration_task, container, false);
        }
        iniViews();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.load(getPJson());
    }

    private String getPJson() {
        TaskPModel model = new TaskPModel();
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(model);
    }

    private void iniViews() {
        ll_task_container = (LinearLayout) contentView.findViewById(R.id.ll_task_container);
        tv_integration_count = (TextView) contentView.findViewById(R.id.tv_integration_count);
        tv_task_in = (TextView) contentView.findViewById(R.id.tv_task_in);
        rl_integration_share_get = (RelativeLayout) contentView.findViewById(R.id.rl_integration_share_get);
        rl_integration_share_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startShareGet(getActivity());
            }
        });
    }

    @Override
    public void loadSuccess(TaskRModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                tv_integration_count.setText(model.money + "");
                tv_task_in.setText("每日任务 0/0");
                if (model.data != null && !model.data.isEmpty()) {
                    initTask(model.data);
                }
            } else {
                ToatUtils.toastSuccess(getActivity(), model.msg);
            }
        }
    }

    private void initTask(ArrayList<TaskRModel.TaskItem> data) {
        if (data == null || data.isEmpty()) return;
        int count = 0;
        ll_task_container.removeAllViews();
        for (int i = 0; i < data.size(); i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.task_item, null, false);
            ImageView icon = (ImageView) view.findViewById(R.id.iv_read_pic);
            ImageView _icon = (ImageView) view.findViewById(R.id.iv_tips);
            TextView tv_name = (TextView) view.findViewById(R.id.tv_task_name);
            TextView tv_em = (TextView) view.findViewById(R.id.tv_task_em);
            initIcon(icon, data.get(i).tid, data.get(i).if_done);
            tv_name.setText("" + data.get(i).name);
            if (data.get(i).if_done == 1) {
                count++;
                tv_name.setTextColor(Color.parseColor("#999999"));
                tv_em.setTextColor(Color.parseColor("#7BFF8D26"));
                _icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.integrate_dark));
            }
            tv_em.setText("" + data.get(i).income);
            ll_task_container.addView(view);
        }
        tv_task_in.setText("每日任务" + count + "/" + data.size());
    }

    private void initIcon(ImageView icon, int tid, int if_done) {
        switch (tid) {
            case 1:
                if (if_done == 1)
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.task_finish));
                else
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.share_award_icon));
                break;
            case 2:
                if (if_done == 1)
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.task_finish));
                else
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.news_comment));
                break;
            case 3:
                if (if_done == 1)
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.task_finish));
                else
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.share));
                break;
            case 4:
                if (if_done == 1)
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.task_finish));
                else
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.my_like_t));
                break;
            case 5:
                if (if_done == 1)
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.task_finish));
                else
                    icon.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.mine_yyiy));
                break;
        }
    }

    @Override
    public void fail(String msg) {

    }
}
